package cn.itrip.auth.controller;


import cn.itrip.auth.service.IItripUserService;
import cn.itrip.beans.dtos.Dto;

import cn.itrip.beans.dtos.InputDto;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.MD5;


import cn.itrip.common.ValidationToken;
import cn.itrip.trade.config.JwtUtil;

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import java.util.Map;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 滕召伟
 * @since 2020-02-27
 */
@CrossOrigin
@Api(tags = "系统用户管理")
@RestController
@RequestMapping("/auth/api")
public class ItripUserController {
    @Resource
    private IItripUserService iUserService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 随机验证码
     */
    String dtoUtil = new DtoUtil().achieveCode();
    /**
     * 赋值验证码
     */
    String verify = dtoUtil;


    @PostMapping(value = "/doregister",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "用户注册",httpMethod = "Post",protocols = "HTTP",produces = "application/json;charset=utf-8",response = InputDto.class,notes = "用户注册")
    public Dto getRegister(@ApiParam(name = "user",value = "系统注册对象",required = true)@RequestBody ItripUser user) {
        //密码加密
        String pwd = MD5.stringToMD5(user.getUserPassword());
        user.setUserPassword(pwd);
        user.setUserType(0);
        user.setActivated(0);
        iUserService.saveUser(user);
        return InputDto.ok();
    }

    @ApiOperation(value = "确认邮箱",httpMethod = "GET",protocols = "HTTP",produces = "application/json;charset=utf-8",response = InputDto.class,notes = "验证是否已存在该邮箱")
    @RequestMapping(value = "/ckusr",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    public Dto getCkusr(@ApiParam(name = "name",value = "邮箱",required = true)@RequestParam(value = "name") String userCode) {

        if (userCode != null || userCode.equals("")) {
            Integer user = iUserService.countCode(userCode);
            if (user == 0) {
                DtoUtil.sendAuthCodeEmail(verify,userCode);
                //邮箱发送
                return InputDto.mes("该邮箱可用");
            } else {
                return InputDto.error("邮箱已被注册");
            }
        } else {
            return InputDto.mes("邮箱为空");
        }
    }

    @ApiOperation(value = "激活邮箱",httpMethod = "Put",protocols = "HTTP",produces = "application/json;charset=utf-8",response = InputDto.class,notes = "激活邮箱")
    @PutMapping(value = "/activate",produces = "application/json;charset=utf-8")
    public Dto saveActivate(@ApiParam(name = "userCode",value = "邮箱",required = true)@RequestParam(value = "user")String userCode,
                            @ApiParam(name = "code",value = "验证码",required = true)@RequestParam(value = "code")String code,Integer activated) {
        if (verify.equals(code)) {
            activated = 1;
            iUserService.updateActivated(userCode,activated);
            return InputDto.mes("激活成功!");
        } else {
            return InputDto.error("验证码不正确!");
        }
    }

    @ApiOperation(value = "用户登陆",httpMethod = "Post",protocols = "HTTP",produces = "application/json;charset=utf-8",response = InputDto.class,notes = "用户登陆")
    @PostMapping(value = "/dologin",produces = "application/json;charset=utf-8")
    public Dto getDologin(@ApiParam(name = "name",value = "邮箱",required = true)@RequestParam(value = "name") String userCode,
                          @ApiParam(name = "password",value = "密码",required = true)@RequestParam(value = "password") String userPassword) {
        String pwd = MD5.stringToMD5(userPassword);
        ItripUser itripUser = new ItripUser();
        itripUser.setUserCode(userCode);
        itripUser.setUserPassword(pwd);
        ItripUser user= iUserService.findUserVoByUsernameAndPassword(itripUser);
        if (user != null) {
            ValidationToken validationToken = new ValidationToken();
            Map<String,Object>  data = validationToken.saveToken(user,redisTemplate);
            return InputDto.data("登陆成功!",data);
        }
        return InputDto.error("账号密码错误!");
    }

    @ApiOperation(value = "注销用户",httpMethod = "Get",protocols = "HTTP",produces = "application/json;charset=UTF-8",response = InputDto.class,notes = "用户注销")
    @GetMapping(value = "/logout",produces = "application/json;charset=utf-8")
    public Dto deleteLogout(HttpServletRequest request){
        // 删除redis缓存数据
        String token = request.getHeader("token");
        redisTemplate.delete(token);
        return InputDto.data("用户退出成功!");
    }


}












